<?php

namespace app\admin\controller;

use app\common\api\controller\Error;
use app\common\controller\Common;
use app\common\model\Menu;
use app\common\model\Manager;
use think\Db;
use think\Validate;
use app\common\api\controller\Relation;
use app\common\api\controller\User as userObj;
use app\common\api\controller\Bonus;

class Home extends Common
{
    protected static $modeType = 2;
    protected static $adminInfo = [];

    protected function _initialize()
    {
        parent::_initialize();
        /*某些公共方法是不需要验证的*/
        $access = [
            'Index' => ['login', 'checkdaydata', 'downrank', 'makecaptcha'],
            'File' => ['uploadpicture'],
            'User' => ['userIndexd']
        ];
        if (!(isset($access[request()->controller()]) && in_array(request()->action(), $access[request()->controller()]))) {
            /*验证是否登录*/
            self::check_load();

            /*左边菜单栏*/
            $MenuObj = new Menu();
            $mid = is_admin_login();
            $adminInfo = Db('manager')->where('mid', $mid)->find();
            self::$adminInfo = $adminInfo;

            $menu = $MenuObj->getMenu($mid);
            $this->assign('mid', $mid);
            $this->assign('menu', $menu);

            $wdNum = Db('withdrawal')->where('status', 0)->count();
            $reNum = Db('recharge')->where('status', 0)->count();

            $this->assign('wdNum', $wdNum);
            $this->assign('reNum', $reNum);

            /*验证管理员是否有权限*/
            $this->isAccess();
        }
    }

    /**
     * 登录验证(只有登录的会员才有信息输出)
     */
    protected function check_load()
    {
        if (is_null(session('admin_auth'))) {

            $this->error('请先登陆', url('index/login'));
        }
        if (is_admin_login() == 0) {
            $auth = session('admin_auth');
            $adminInfo = Db('manager')->where('mid', $auth['mid'])->find();
            /*登录会员数据输出*/
            $this->assign('mid', $auth['mid']);
            $this->assign('admininfo', $adminInfo);
        }
    }

    /**
     * 检查权限
     * @return bool
     */
    private function isAccess()
    {
        $controller = request()->controller();
        $action = request()->action();
        $url = $controller . '/' . $action;
        $id = Db::name('menu')->where('url', $url)->value('id');
        if (!$id) {
            return true;
        }
        $mid = is_admin_login();
        $mname = Db::name('manager')->where('mid', $mid)->value('mname');
        $first = model('manager')->order('mid asc')->find();
        if ($mname != $first->mname) {
            $menuObj = new Menu();
            $rules = $menuObj->get_rules($mid);
            if (!in_array($id, $rules)) {
                $this->error('权限不足！', url('Index/login'));
            }
        }
    }

    /**
     * 会员注册
     */
    public function registerAdmin()
    {
        if (!request()->isPost()) {
            $this->error('非法操作！');
        }

        $rules = [
            'mobile' => 'require|length:11',
//            'tuijiannumber' => 'require',
            'password1' => 'require|min:6',
            'password2' => 'require|min:6',
//            'repassword' => 'required|min:6',
        ];
        $messages = [
            'mobile.require' => '手机号码必填',
            'mobile.length' => '手机号码为11位',
//            'tuijiannumber.require' => '推荐人账号必填',
            'password1.require' => '登录密码必填',
            'password1.min' => '登录密码不能少于6位数',
            'password2.require' => '资产密码必填',
            'password2.min' => '资产密码不能少于6位数',
//            'repassword.required' => '确认密码必填',
//            'repassword.min' => '确认密码不能少于6位数'
        ];
        $data = input();

        $validate = new Validate($rules, $messages);
        $result = $validate->check($data);
        if (!$result) {
            $this->error($validate->getError());
        }

        $first = Db('member')->order('uid asc')->find();
        if ($first) {
            if ($data['tuijiannumber']) {
                $tusernumber = Db('member')->where('usernumber', $data['tuijiannumber'])->value('usernumber');
                if (!$tusernumber) {
                    $this->error('推荐人不存在！');
                }
            } else {
                $tusernumber = $first['usernumber'];
            }

            $relationApi = new Relation();
            $tdata = $relationApi->tRelation($tusernumber);
        }


        //注册成功
        $userdata = array(
            'usernumber' => $data['mobile'],
            'mobile' => $data['mobile'],
            'realname' => $data['realname'],
            'psd1' => $data['password1'],
            'psd2' => $data['password2'],
        );

        /*判断是否存在推荐关系*/
        if (isset($tdata)) {
            if ($tdata['code'] > 0) {
                unset($tdata['code']);
                $userdata = array_merge($userdata, $tdata);
            } else {
                $this->error(Error::regError($tdata['code']));
            }
        }

        $UserApi = new userObj();
        $res = $UserApi->register($userdata);

        if ($res['status'] > 0) {
            $this->success('注册成功');
        } else {
            $this->error('注册失败');
        }
    }
}